#include <assert.h>
#include <imbus.h>
#include <stdio.h>
#include "im_internals.h"

int main (int argc, char *argv [])
{
    IMIOChannel *channel;
    IMUChar     *ptr;
    IMSize       i;
    IMSize       written;
    IMIOError    error;

    im_init(&argc, &argv);

#if IM_ENABLE_DEBUG
    _im_slice_print_debug_info();
#endif

    channel = im_io_channel_new_file ("iochannel-test-file", "w+", &error);
    if (!channel) {
        fprintf (stderr, "Failed to open file: %d\n", error);
        return 1;
    }
    im_io_channel_set_close_on_unref(channel, TRUE);

    for (i=0; i < 1048576; ++i) {
        if (!im_io_channel_write(channel, (IMChar*)(&i), sizeof(i), &written, &error)) {
            fprintf (stderr, "Failed to write to channel: %d\n", error);
            im_object_unref(channel);
            return 1;
        }
    }

    ptr = im_io_channel_mmap (channel, 4200, 1024, FALSE, FALSE, &error);
    if (!ptr) {
        fprintf (stderr, "Failed to mmap channel: %d\n", error);
        im_object_unref(channel);
        return 1;
    }

    printf ("Number at ptr %p: %d\n", ptr, *((IMSize*)ptr));
    if (!im_io_channel_munmap (channel, ptr, 1024, &error)) {
        fprintf (stderr, "Failed to munmap channel: %d\n", error);
        im_object_unref(channel);
        return 1;
    };

    im_object_unref(channel);

#if IM_ENABLE_DEBUG
    _im_slice_print_debug_info();
#endif

    return 0;
}
/*
vi:ts=4:nowrap:ai:expandtab
*/
